var DefaultColor_1 = "#ffffff";
var DefaultColor_2 = "#F9FCFF";
var MarkColor      = "#ff0000";
var PointerColor   = "#00ff00";

var useTableCheckBox = true;

var idTable		   = "myTable";

initTableTr(idTable);

function initTableTr(id)
{
	var oTableTr = document.getElementById(id).getElementsByTagName("tr");
	
	for (var i = 0; i < oTableTr.length; i++) 
	{
		num = i % 2;

		if( num == 0 )
		{
			//oTableTr[i].setAttribute("bgColor1", DefaultColor_1);
			//oTableTr[i].setAttribute("bgColor2", DefaultColor_1);
			oTableTr[i].bgColor = DefaultColor_1;
		}
		else
		{
			//oTableTr[i].setAttribute("bgColor1", DefaultColor_2);
			//oTableTr[i].setAttribute("bgColor2", DefaultColor_2);
			oTableTr[i].bgColor = DefaultColor_2;
		}

		//oTableTr[i].onmouseover = itemOver;
		//oTableTr[i].onmouseout  = itemOut;
		oTableTr[i].onmousedown = itemDown;
		
		if( useTableCheckBox )
		{
			oTableTr[i].setAttribute("index", i);
			
			var oInput = oTableTr[i].getElementsByTagName("input");
			
			for( var j=0; j<oInput.length; j++ )
			{
				if( oInput[j].type == "checkbox" )
				{
					oInput[j].id = "chk_row_"+i;
					oInput[j].checked = false;
					//document.getElementById("chk_icon_"+i).src = chkIconFalse;
				}
			}
			
			var oIcon = oTableTr[i].getElementsByTagName("img");
			
			for( var j=0; j<oIcon.length; j++ )
			{
				if( oIcon[j].className == "chkIcon" )
				{
					oIcon[j].id = "chk_icon_"+i;
					oIcon[j].src = chkIconFalse;
				}
			}
		}
	}
	
	function itemOver() 
	{
		if( this.bgColor != PointerColor )
			this.bgColor = MarkColor;
	}

	function itemOut() 
	{
		if ( this.bgColor == PointerColor ) return;

		this.bgColor = (this.getAttribute("index") % 2 == 0) ? DefaultColor_1 : DefaultColor_2;
	}

	function itemDown() 
	{
		//document.getElementById("selected-all").checked = false;
		//document.getElementById("selected-all-icon").src = chkIconFalse;
		/*	
		if ( this.bgColor == PointerColor )
		{
			this.bgColor = (this.getAttribute("index") % 2 == 0) ? DefaultColor_1 : DefaultColor_2;
		}
		else
		{
			this.bgColor = PointerColor
		}
		*/
		if( useTableCheckBox )
		{
			var oCheckBox = document.getElementById("chk_row_"+this.getAttribute("index"));
			var oChkIcon  = document.getElementById("chk_icon_"+this.getAttribute("index"));
			//var oSelected  = document.getElementById("selected");
	
			if( oCheckBox != null )
			{
				if( oCheckBox.checked )
				{
					oCheckBox.checked = false;
					oChkIcon.src = chkIconFalse;
					
					//position = oSelected.value.indexOf(oCheckBox.value + ",");
					//if(position != -1 )
					//{
					//	oSelected.value = oSelected.value.substring(0, position) + oSelected.value.substring(position+(oCheckBox.value + ",").length, oSelected.value.length);
					//}
				}
				else
				{
					oCheckBox.checked = true;
					oChkIcon.src = chkIconTrue;
					//oSelected.value = oSelected.value + oCheckBox.value + ",";
				}
			}
		}
	}
}

function selectAll(id)
{
	var oCheckAll =  document.getElementById("selected-all");
	var oIconAll =  document.getElementById("selected-all-icon");
	var oTableTr = document.getElementById(id).getElementsByTagName("tr");
	
	//alert(oCheckAll.checked);
	
	if( ! oCheckAll.checked )
	{
		oCheckAll.checked = true;
		oIconAll.src = chkIconTrue;
		oChecked = true;
		oIconSrc = chkIconTrue;
	}
	else
	{
		oCheckAll.checked = false;
		oIconAll.src = chkIconFalse;
		oChecked = false;
		oIconSrc = chkIconFalse;
	}
	
	for (var i = 0; i < oTableTr.length; i++) 
	{
		oTableTr[i].bgColor = oCheckAll.checked ? PointerColor : ((i % 2 == 0) ? DefaultColor_1 : DefaultColor_2);
		
		var oCheckBox = document.getElementById("chk_row_"+i);
		var oChkIcon  = document.getElementById("chk_icon_"+i);

		if( oCheckBox != null )
		{
			oCheckBox.checked = oChecked;
			oChkIcon.src = oIconSrc;
		}
	}
}